/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.vsmanager;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

import ws.y10k.vi.Host;
import ws.y10k.vi.HostAlreadyExistsException;
import ws.y10k.vi.HostDataEmptyException;
import ws.y10k.vi.HostProtocol;
import ws.y10k.vi.VIVersionNotSupported;

/**
 * @author natx
 *
 */
public class ConnectButtonSelectionListener implements SelectionListener {
	
	AddHostWindow window;
	
	public ConnectButtonSelectionListener(AddHostWindow w) {
		this.window = w;
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) { }

	@Override
	public void widgetSelected(SelectionEvent arg0) {
		// TODO i18n
		if ( window.txtHostname.getText().isEmpty() || window.txtUser.getText().isEmpty() || window.txtPassword.getText().isEmpty() ) {
			MessageDialog.openError(window.getShell(), "Not enough information", "You must specify hostname, user and password");
		} else {
			Host srvNew = null;
			try {
			if ( window.btnProtocolHTTP.getSelection() )
				srvNew = new Host(window.txtHostname.getText(),window.txtUser.getText(),window.txtPassword.getText(),HostProtocol.HTTP);
			else
				srvNew = new Host(window.txtHostname.getText(),window.txtUser.getText(),window.txtPassword.getText());

			} catch (RemoteException e) {
				MessageDialog.openError(window.getShell(), 
						"Connection Error", 
						"Some connection error occurred: " + e.toString());
			} catch (MalformedURLException e) {
				MessageDialog.openError(window.getShell(), 
						"Host connection information invalid", 
						"The data you provided caused an error forming the URL. Please check it or report a bug.");
			} catch (HostDataEmptyException e) {
				MessageDialog.openError(window.getShell(), 
						"Host connection information incorrect", 
						"The data you provided is incomplete or gives empty values.");
			} catch (VIVersionNotSupported e) {
				MessageDialog.openError(window.getShell(), 
						"VMWare version not supported", 
						"This version of VMWare product is not supported: " + e.getSoftware());
			}
			if ( srvNew != null ) {
				try {
					VSManager.addHost(srvNew);
				} catch (HostAlreadyExistsException e) {
					MessageDialog.openError(window.getShell(), 
							"Host connection already exists", 
							"The host connection you're trying to add already exists.");
				}
				window.txtHostname.setText("");
				window.txtUser.setText("");
				window.txtPassword.setText("");
				window.txtHostname.setFocus();
			}
		}
	}

}
